package com.cc.moxingvoice.lock;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.cc.moxingvoice.base.BaseLock;
import com.cc.moxingvoice.constant.Connection;
import com.cc.moxingvoice.constant.MyUrl;
import com.cc.moxingvoice.databinding.FragmentForumBinding;
import com.cc.moxingvoice.response.GetUrlResponse;

import java.util.HashMap;

/**
 * 创建日期：2018/3/25 on 15:26
 * 描述：
 * 作者：郭士超
 * QQ：1169380200
 */

public class ForumLock extends BaseLock<FragmentForumBinding> {

    public ForumLock(Context context, FragmentForumBinding binding) {
        super(context, binding);
    }

    public ForumLock(Context context, FragmentForumBinding binding, Bundle bundle) {
        super(context, binding, bundle);
    }

    @Override
    protected void init() {
        initWebView();
        getUrl();
    }

    private void initWebView() {
        //WebView加载web资源
        mBinding.webView.loadUrl(MyUrl.SHARE_URL);
        // 不让跳到浏览器中
        mBinding.webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;// 返回false
            }
        });
        //启用支持Javascript
        WebSettings settings = mBinding.webView.getSettings();
        settings.setJavaScriptEnabled(true);
    }

    private void getUrl() {
        // 访问网络的方法
        HashMap hashMap = new HashMap();
        Connection.getInstance().post(GetUrlResponse.class, MyUrl.GET_FORUM_URL, hashMap, new Connection.ResponseListener() {
            @Override
            public void tryReturn(int id, Object response) {
                final GetUrlResponse result = (GetUrlResponse) response;
                switch (id) {
                    case 200:
                        mBinding.webView.clearHistory(); // 清除
                        mBinding.webView.loadUrl(result.getData().getUrl());
                        break;
                    case -2:

                        break;
                    default:

                        break;
                }
            }
        });
    }


}
